﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Windows.Forms;


namespace BLL
{
    /// <summary>
    /// 获取Account数据保存的位置，并实现保存、读取、添加、更新、删除的功能
    /// </summary>
    public class AccountManagement
    {
       // private static string dirOfAccountListFile = "\\account";
       // private static string pathOfAccountListFile = "\\account.xml";
        private AccountCollection accountList = new AccountCollection();

        public AccountManagement()
        {
        }

        /// <summary>
        /// 获取account文件夹的绝对路径(account文件夹中保存着account.xml)
        /// </summary>
        public static string DirOfAccountListFile
        {
            get
            {
                string dir = Application.StartupPath + "\\account";
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                return dir;
            }
        }

        /// <summary>
        /// 获取account.xml文件的绝对路径
        /// </summary>
        public static string PathOfAccountListFile
        {
            get
            {
                string path = DirOfAccountListFile + "\\account.xml";
                if (!File.Exists(path))
                {
                    FileStream fs = null;
                    try
                    {
                        fs = File.Create(path);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    finally
                    {
                        if (fs != null)
                        {
                            fs.Close();
                        }
                    }
                }
                return path;
            }
        }


        /// <summary>
        /// 获取或设置帐户列表
        /// </summary>
        public AccountCollection AccountList
        {
            get
            {
                return accountList;
            }
            set
            {
                accountList = value;
            }
        }

        /// <summary>
        /// 将数据保存到account.xml
        /// </summary>
        public bool SaveDataToFile()
        {
            StreamWriter sw = new StreamWriter(AccountManagement.PathOfAccountListFile, false, Encoding.Default);
            XmlSerializer xml = new XmlSerializer(typeof(AccountCollection));
            try
            {
                xml.Serialize(sw, accountList);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return false;
            }
            finally
            {
                if (sw != null)
                {
                    sw.Close();
                }
            }

            return true;
        }

        /// <summary>
        /// 从account.xml中读取数据
        /// </summary>
        public bool LoadDataFromFile()
        {
            StreamReader sr = new StreamReader(AccountManagement.PathOfAccountListFile, Encoding.Default);
            XmlSerializer xml = new XmlSerializer(typeof(AccountCollection));
            try
            {
                accountList = (AccountCollection)xml.Deserialize(sr);
            }
            catch //(Exception ex)
            {
                //MessageBox.Show(ex.ToString());
                return false;
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                }
            }

            return true;
        }

        /// <summary>
        /// 将一个新账户保存入account.xml文件和AccountManagement.AcccountList中
        /// </summary>
        ///<param name="acc" Account类的对象></param>
        public void AddAccount(Account acc)
        {
            this.AccountList.Add(acc);
            this.SaveDataToFile();
        }
        /// <summary>
        /// 将该ID的账户从account.xml文件和AccountManagement.AcccountList中删除
        /// </summary>
        ///<param name="iID" 该账户对应ID></param>
        public void DelAccount(int iID)
        {
            foreach (Account account in this.AccountList)
            {
                if (account.ID == iID)
                {
                    this.AccountList.Remove(account);
                    break;     //每次删除操作都会影响AccountList，导致无法继续foreach循环，因此每次只能执行一次删除操作
                }
            }
            this.SaveDataToFile();
        }

        /// <summary>
        /// 将该ID的账户在account.xml文件和AccountManagement.AcccountList中更新
        /// </summary>
        ///<param name="acc" Account类的对象></param>
        public void UpdateAccount(Account acc)
        {
            int icount = 0, iwhere = 0;
            foreach (Account account in this.AccountList)
            {
                if (account.ID == acc.ID)
                {
                    iwhere = icount;
                    this.AccountList[iwhere].Balance = acc.Balance;
                    this.AccountList[iwhere].Color = acc.Color;
                    this.AccountList[iwhere].CreatedTime = acc.CreatedTime;
                    this.AccountList[iwhere].ID = acc.ID;
                    this.AccountList[iwhere].ModifiedTime = acc.ModifiedTime;
                    this.AccountList[iwhere].Name = acc.Name;
                    this.SaveDataToFile();
                    break;     //每次删除操作都会影响AccountMag.AccountList，导致无法继续foreach循环，因此每次只能执行一次删除操作
                }
                icount++;
            }

        }

        public Account GetAccount(int iID)
        {
            foreach (Account account in this.AccountList)
            {
                if (account.ID == iID)
                {
                    return account;
                }
            }
            return null;
        }

        //获取该Account在AccountList中的索引值
        public int GetAccountIndex(int iID)
        {
            int iIndex;
            for (iIndex = 0; iIndex < AccountList.Count; iIndex++)
            {
                if (AccountList[iIndex].ID == iID)
                {
                    return iIndex;
                }
            }
            return -1;
        }    

        /// <summary>
        /// 检查账户名是否已存在
        /// </summary>
        ///<param name="acc" Account类的对象></param>
        public bool IsNameExist(Account acc) 
        {
            foreach (Account account in this.AccountList) 
            {
                if (account.Name == acc.Name) 
                {
                    return true;
                }
               
            }
            return false;
        }

    }
}
